<template>
  <Form
    :label-width="145"
    ref="formValidate"
    :rules="ruleValidate"
    :model="formValidate"
    label-position="left"
    :label-colon="true"
  >

    <FormItem label="单位名称" prop="orgName">
      <i-input
        v-show="type !== 3"
        type="text"
        v-model="formValidate.orgName"
        placeholder="单位名称"

      >
      </i-input>
      <span v-show="type === 3">{{formValidate.orgName}}</span>
    </FormItem>
    <FormItem label="上级单位">
      <Select v-model="formValidate.fkParentId" v-show="type !== 3">
        <Option v-for="item in topUnitList" :value="item.id" :key="item.id">{{ item.orgName }}</Option>
      </Select>
      <span v-show="type === 3">{{unitName}}</span>
    </FormItem>
    <FormItem prop="contacts" label="联系人">
      <i-input
      v-show="type !== 3"
        type="text"
        v-model="formValidate.contacts"
        placeholder="联系人"
      >
      </i-input>
      <span v-show="type === 3">{{formValidate.contacts}}</span>
    </FormItem>
    <FormItem prop="phone" label="联系电话">
      <i-input
        type="text"
        v-show="type !== 3"
        v-model="formValidate.phone"
        placeholder="联系电话"
      >
      </i-input>
       <span v-show="type === 3">{{formValidate.phone}}</span>
    </FormItem>
    <FormItem prop="email" label="单位邮箱">
      <i-input
        type="text"
        v-show="type !== 3"
        v-model="formValidate.email"
        placeholder="单位邮箱"
      >
      </i-input>
       <span v-show="type === 3">{{formValidate.email}}</span>
    </FormItem>
    <FormItem prop="fax" label="单位传真">
      <i-input
        type="text"
        v-show="type !== 3"
        v-model="formValidate.fax"
        placeholder="单位传真"
      >
      </i-input>
       <span v-show="type === 3">{{formValidate.fax}}</span>
    </FormItem>
    <FormItem prop="address" label="单位地址">
      <i-input
        type="text"
        v-show="type !== 3"
        v-model="formValidate.address"
        placeholder="单位地址"
      >
      </i-input>
       <span v-show="type === 3">{{formValidate.address}}</span>
    </FormItem>
    <FormItem prop="orgCode" label="单位编号">
      <i-input
        type="text"
        v-show="type !== 3"
        v-model="formValidate.orgCode"
        placeholder="单位编号"
      >
      </i-input>
       <span v-show="type === 3">{{formValidate.orgCode}}</span>
    </FormItem>
    <FormItem prop="socialCreditCode" label="社会统一信用代码">
      <i-input
        type="text"
        v-show="type !== 3"
        v-model="formValidate.socialCreditCode"
        placeholder="社会统一信用代码"
      >
      </i-input>
       <span v-show="type === 3">{{formValidate.socialCreditCode}}</span>
    </FormItem>
    <FormItem prop="bankName" label="开户银行名称">
      <i-input
        type="text"
        v-show="type !== 3"
        v-model="formValidate.bankName"
        placeholder="开户银行名称"
      >
      </i-input>
       <span v-show="type === 3">{{formValidate.bankName}}</span>
    </FormItem>
    <FormItem prop="bankAccount" label="银行账号">
      <i-input
        type="text"
        v-show="type !== 3"
        v-model="formValidate.bankAccount"
        placeholder="银行账号"
      >
      </i-input>
       <span v-show="type === 3">{{formValidate.bankAccount}}</span>
    </FormItem>
    <FormItem prop="legalPerson" label="法人">
      <i-input
        type="text"
        v-show="type !== 3"
        v-model="formValidate.legalPerson"
        placeholder="法人"
      >
      </i-input>
       <span v-show="type === 3">{{formValidate.legalPerson}}</span>
    </FormItem>
    <FormItem prop="memo" label="备注">
      <i-input
        type="textarea"
        v-show="type !== 3"
        v-model="formValidate.memo"
        placeholder="备注"
      >
      </i-input>
       <span v-show="type === 3">{{formValidate.memo}}</span>
    </FormItem>
    <FormItem prop="fkLogoId" label="logo">
      <i-input
        type="text"
        v-show="type !== 3"
        v-model="formValidate.fkLogoId"
        placeholder="logo"
      >
      </i-input>
      <span v-show="type === 3">{{formValidate.orgCode}}</span>
    </FormItem>
     <FormItem prop="memo" label="创建人" v-show="type === 3">
       <span>{{formValidateView.createUserName}}</span>
    </FormItem>
    <FormItem prop="memo" label="创建时间" v-show="type === 3">
       <span>{{formValidateView.gmtCreate}}</span>
    </FormItem>
    <FormItem prop="memo" label="修改人" v-show="type === 3">
       <span>{{formValidateView.updateUserName}}</span>
    </FormItem>
    <FormItem prop="memo" label="修改时间" v-show="type === 3">
       <span>{{formValidateView.gmtModified}}</span>
    </FormItem>
    <FormItem>
      <Button
        type="primary"
        @click="publicSubmit('formValidate')"
        v-if="type !== 3"
      >确定</Button>
      <Button @click="publicReset('formValidate')" style="margin-left: 8px">取消</Button>
    </FormItem>
  </Form>
</template>

<script>
import { updateMixin } from '../../../mixin'
import API_KEY from '../../../api'
import { isPhone, isEmail } from '../../../utils/verification'
export default {
  name: 'UnitUpdate',
  mixins: [updateMixin],
  props: {
    topUnitList: {
      type: Array,
      default: () => { return [] }
    }
  },
  computed: {
    unitName () {
      const arr = this.topUnitList.filter(item => item.id === this.formValidate.fkParentId)
      if (arr.length > 0) {
        return arr[0].orgName
      }
      return ''
    }
  },
  data () {
    return {
      // 表单数据
      formValidate: {
        id: '',
        orgName: '',
        address: '',
        contacts: '',
        phone: '',
        fax: '',
        fkParentId: '',
        orgCode: '',
        email: '',
        bankName: '',
        bankAccount: '',
        legalPerson: '',
        socialCreditCode: '',
        memo: '',
        fkLogoId: ''
      },
      // 表单校验规则
      ruleValidate: {
        orgName: [{ required: true, message: '当前项不能为空', trigger: 'blur' }],
        orgCode: [{ required: true, message: '当前项不能为空', trigger: 'blur' }],
        phone: [{
          validator: (rule, value, callback) => {
            if (value === '' || isPhone(value) || this.formValidate.phone == null) {
              callback()
            } else {
              callback(new Error('手机号码格式错误'))
            }
          },
          trigger: 'blur'
        }],
        email: [{
          validator: (rule, value, callback) => {
            if (value === '' || isEmail(value) || this.formValidate.email == null) {
              callback()
            } else {
              callback(new Error('邮箱地址格式错误'))
            }
          },
          trigger: 'blur'
        }]
      },
      action: {
        // 根据id获取数据（如果使用混入，属性名不能变，所在位置不能变,下同）
        readDataById: API_KEY.unit.def,
        // 新增数据
        createData: API_KEY.unit.def,
        // 更新数据
        updateData: API_KEY.unit.def
      }

    }
  }

}
</script>

<style scoped>

</style>
